﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using TYAJZWeb.Utils;
using TYAJZWeb.Models;
using Newtonsoft.Json;

namespace TYAJZWeb.Controllers
{
    public class AJZInfoController : BaseController
    {
        //
        // GET: /Print/
        [CookiesAuthorize]
        [HttpGet]
        public ActionResult Edit()
        {
            AJZInfoEditModel model = HttpContext.Application[SystemVariableEnums.AJZInfo.ToString()] as AJZInfoEditModel;
            return View(model);
        }

        [CookiesAuthorize]
        [HttpPost]
        public ActionResult Edit(AJZInfoEditModel model)
        {
            string str = JsonConvert.SerializeObject(model);
            HttpContext.Application[SystemVariableEnums.AJZInfo.ToString()] = model;
            string typename = SystemVariableEnums.AJZInfo.ToString();
            var s = Context.SystemVariables.SingleOrDefault(t => t.Type == typename);
            if (s == null)
            {
                s = new Data.SystemVariable()
                {
                    CreateDT = DateTime.Now,
                    LastModifiedUser = CurrentUserId,
                    ModifiedDT = DateTime.Now,
                    Type = SystemVariableEnums.AJZInfo.ToString(),
                    Value = string.Empty
                };
                Context.SystemVariables.InsertOnSubmit(s);
                Context.SubmitChanges();
                s = Context.SystemVariables.Single(t => t.Type == typename);
            }
            s.ModifiedDT = DateTime.Now;
            s.LastModifiedUser = CurrentUserId;
            s.Value = str;
            Context.SubmitChanges();

            TempData.Add("Success", "设置成功");

            if (System.Web.HttpContext.Current.Application.Get(typename) != null)
                System.Web.HttpContext.Current.Application.Remove(typename);
            System.Web.HttpContext.Current.Application.Add(typename, JsonConvert.DeserializeObject<AJZInfoEditModel>(s.Value));

            return RedirectToAction("Edit");
        }
    }
}
